package com.iot08.api.service;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.tea.utils.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iot08.api.config.redis.RedisFilterService;
import com.iot08.api.constant.Constant;
import com.iot08.api.response.VimMemberResp;
import com.iot08.api.response.VipPrivilegeResp;
import com.iot08.api.vo.request.CreatePaymentReqBodyFormVO;
import com.iot08.api.vo.request.VipMemberOpenReqBodyFormVo;
import com.iot08.api.vo.request.base.BodyFormAccount;
import com.iot08.common.entity.app.FirstRechargeRenewRemindSetting;
import com.iot08.common.entity.app.ProductInfo;
import com.iot08.common.entity.app.VipMember;
import com.iot08.common.entity.app.VipPrivilege;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.DateUtil;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * @author cwj
 * @description
 * @date 2024/12/29 9:39
 **/
@Slf4j
@Service
public class VipMemberService extends BaseService<VipMember> {
    
    @Value("${complaint.domain}")
    private String domain;

    @Resource
    private RedisFilterService redisFilterService;

    @Autowired
    @Lazy
    private ProductInfoService productInfoService;

    @Resource
    private BrokenScreenInsuranceService brokenScreenInsuranceService;

    @Resource
    private FirstRechargeRenewRemindSettingService firstRechargeRenewRemindSettingService;

    @Resource
    private VipPrivilegeService vipPrivilegeService;

    /**
     * @description 开通会员
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2024/12/29 16:24
     **/
    public ResponseMap openVipMember(VipMemberOpenReqBodyFormVo vo){
        VipMember vipMember = queryVipMemberByOpenId(vo.getOpenid());
        if(ObjectUtils.isEmpty(vipMember)){
            //保存vip信息
            return saveVipMember(vo);
        } else {
            return updateVipMember(vo, vipMember.getId());
        }
    }

    /**
     * @description 创建或更新会员信息
     * @author cwj
     * @param [vo, orderId]
     * @return void
     * @date @time 2024/12/30 17:06
     **/
    public void createOrUpdateVipMember(CreatePaymentReqBodyFormVO vo, Integer productType, String orderId) {
        VipMember vipMember = this.buildVipMember(vo.getOpenid(), orderId,vo.getPid(), vo.getIsAgreeVipProtocol(), vo.getGrade(),
                productType,0,0);
        this.saveOrUpdateVipMember(vipMember);
    }

    /**
     * @param [vo]
     * @description 保存或更新会员信息
     * @author cwj
     * @date @time 2024/12/30 16:49
     **/
    public void saveOrUpdateVipMember(VipMember vo){
        //根据openid查询会员记录
        VipMember vipMember = queryVipMemberByOpenId(vo.getOpenid());
        if(ObjectUtils.isEmpty(vipMember)){
            //保存vip信息
            this.save(vo);
        } else {
            //会员存在，则先暂存会员更新的信息，等支付成功了，再进行会员信息更新
            JSONObject vm = new JSONObject();
            vm.put("orderid",vo.getOrderid());
            vm.put("grade",vo.getGrade());
            vm.put("type",vo.getType());
            vm.put("openid",vo.getOpenid());
            vm.put("pid",vo.getPId());
            vm.put("isAgreeVipProtocol",vo.getIsAgreeVipProtocol());
            vm.put("isOnTrialVip",vo.getIsOnTrialVip());
            redisFilterService.setex(vo.getOrderid() + Constant.REDIS_VIP_MEMBER_POSTFIX,3*60*60,vm.toJSONString());
            //支付成功了再更新
           /* Update update = buildUpdate(vo.getOrderid(),vo.getGrade(),vo.getType(),null,vo.getIsAgreeVipProtocol());
            this.updateById(new ObjectId(vipMember.getId()),update);*/
        }
    }


    /**
     * @description 创建或更新赠送会员
     * @author cwj
     * @param [openid, duration]
     * @return void
     * @date @time 2025/2/13 14:16
     **/
    public void presentVipMember(String openid,Long duration) {
        if(!StringUtils.isEmpty(openid)){
            VipMember vipMember = queryVipMemberByOpenId(openid);
            ProductInfo productInfo = productInfoService.findOne(new Query(Criteria.where("type").is(1)).limit(1));
            long currentTime = System.currentTimeMillis()/1000;
            if(ObjectUtils.isEmpty(vipMember)){
                vipMember = buildDefaultVipMember(openid, duration, productInfo, currentTime);
                //保存vip信息
                this.save(vipMember);
            } else {
                //会员存在，则先暂存会员更新的信息，等支付成功了，再进行会员信息更新
                long endTime = vipMember.getEndTime();
                Update update = new Update();
                update.set("isVip",1);
                update.set("type",productInfo.getType());
                update.set("pId",productInfo.getId());
                update.set("isAgreeVipProtocol",1);
                update.set("isOnTrialVip",1);
                if(endTime < currentTime){
                    update.set("endTime",currentTime+duration);
                } else {
                    update.set("endTime",endTime+duration);
                }
                update.set("updateTime",new Date());
                this.updateById(new ObjectId(openid),update);
            }
        }
    }

    /**
     * @description 查询会员新，不论会员是否有效
     * @author cwj
     * @param [openid]
     * @return com.iot08.common.entity.app.VipMember
     * @date @time 2025/2/19 14:49
     **/
    public VipMember queryVipMemberByOpenId(String openid) {
        return this.findOne(new Query(Criteria.where("openid").is(openid)));
    }


    /**
     * @description 构建默认VipMember对象
     * @author cwj
     * @param [openid, duration, productInfo, currentTime]
     * @return com.iot08.common.entity.app.VipMember
     * @date @time 2025/2/13 14:26
     **/
    private VipMember buildDefaultVipMember(String openid, Long duration, ProductInfo productInfo, long currentTime) {
        VipMember vipMember;
        vipMember = new VipMember();
        vipMember.setType(productInfo.getType());
        vipMember.setGrade(1);
        vipMember.setPId(productInfo.getId());
        vipMember.setOpenid(openid);
        vipMember.setIsVip(1);
        vipMember.setIsOnTrialVip(1);
        vipMember.setIsAgreeVipProtocol(1);
        vipMember.setBeginTime(currentTime);
        vipMember.setEndTime(currentTime + duration);
        vipMember.setCreateTime(new Date());
        vipMember.setUpdateTime(new Date());
        return vipMember;
    }


    /**
     * @description 更新会员的状态
     * @author cwj
     * @param [orderid, isVip]
     * @return boolean
     * @date @time 2024/12/30 17:31
     **/
    public void updateVipMemberInfo(String orderId,Integer isVip)  {
        Query query;
        Update update = new Update();
        update.set("isVip",isVip);
        update.set("updateTime",new Date());
        //查询缓存信息
        String cacheData = redisFilterService.get(orderId + Constant.REDIS_VIP_MEMBER_POSTFIX);
        if(!StringUtils.isEmpty(cacheData)){
            //之前买过会员的
            try {
                log.info("更新会员信息......");
                ObjectMapper objectMapper = new ObjectMapper();
                //取出缓存的数据
                JSONObject dataCache = objectMapper.readValue(cacheData, JSONObject.class);
                Integer type = dataCache.getInteger("type");
                query = new Query(Criteria.where("openid").is(dataCache.getString("openid"))).limit(1);
                long currentTimeMillis = System.currentTimeMillis();
                long currentTime = currentTimeMillis/1000;
                VipMember vipMember = this.findOne(query);
                //会员信息存在
                if(ObjectUtils.isNotEmpty(vipMember)) {
                    String oldOrderid = vipMember.getOrderid();
                    update.set("orderid",dataCache.getString("orderid"));
                    update.set("grade",dataCache.getInteger("grade"));
                    update.set("type",type);
                    update.set("pId",dataCache.getString("pid"));
                    //支付之后，用户就不再是免费会员了
                    update.set("isOnTrialVip",ObjectUtils.isEmpty(dataCache.getInteger("isOnTrialVip"))?0:dataCache.getInteger("isOnTrialVip"));
                    long endTime = vipMember.getEndTime();
                    //之前的会员还没结束，则续签会员
                    if(vipMember.getIsVip().equals(1) && endTime>currentTime){
                        update.set("endTime",transferEndTime(type,endTime));
                    } else {
                        update.set("endTime",transferEndTime(type,currentTime));
                    }
                    //更新会员信息
                    this.updateById(new ObjectId(vipMember.getId()),update);
                    //清除之前可能存在的缓存信息，当下缓存等到下一个会员日充值时再清除,避免缓存过多
                    if(!StringUtils.isEmpty(redisFilterService.get(oldOrderid + Constant.REDIS_VIP_MEMBER_POSTFIX))){
                        redisFilterService.del(oldOrderid + Constant.REDIS_VIP_MEMBER_POSTFIX);
                    }
                    //创建碎屏险
                    brokenScreenInsuranceService.createBrokenScreenInsurance(orderId, vipMember.getOpenid(), type, currentTime, currentTimeMillis,1,0,0);
                } else {
                    //会员信息不存在，保存会员信息
                    vipMember = buildVipMember(dataCache.getString("openid"),dataCache.getString("orderid"), dataCache.getString("pid"),
                           dataCache.getInteger("isAgreeVipProtocol"),dataCache.getInteger("grade"),type,isVip,0);
                    this.save(vipMember);
                    //创建碎屏险
                    brokenScreenInsuranceService.createBrokenScreenInsurance(orderId, vipMember.getOpenid(), type, currentTime, currentTimeMillis,1,0,0);
                }
                //支付成功了，清除当下下单时的缓存
                redisFilterService.del(orderId + Constant.REDIS_VIP_MEMBER_POSTFIX);
            } catch (JsonProcessingException je){
                log.error("会员更新失败，发生异常，异常信息为：{}",je.getMessage());
            }
        } else {
            //更新会员信息
            query = new Query(Criteria.where("orderid").is(orderId));
            this.update(query,update);
        }
    }





    /**
     * @description 更新会员的有效时间
     * @author cwj
     * @param [openid, productType]
     * @return void
     * @date @time 2025/2/18 16:21
     **/
    public void updateVipMemberEffectTimeTypeOrderId(VipMember vipMember,Integer productType,boolean isDeleteCurrentOrder,
                                                     String secondOrderId,Integer secondProductType){
        /* Query query = new Query(Criteria.where("openid").is(openid)).limit(1);
         VipMember vipMember = this.findOne(query);*/
         if(!ObjectUtils.isEmpty(vipMember)) {
             Update update = new Update();
             Long endTime = vipMember.getEndTime();
             long currentTime = System.currentTimeMillis()/1000;
             //会员更新时间为则将其设置到期时间为昨天
             if(ObjectUtils.isEmpty(endTime)){
                 endTime = currentTime - 24*60*60;
             } else {
                 //会员到期时间减少相应产品类型的时间
                 endTime = cutDownVipEndTime(productType, endTime);
             }
             //删除当前订单且前面仍有有效支付的会员订单，且订单在有效期内
             if(isDeleteCurrentOrder) {
                 if (!StringUtils.isEmpty(secondOrderId)){
                     update.set("orderid",secondOrderId);
                 }
                 if(!ObjectUtils.isEmpty(secondProductType)){
                     update.set("type",secondProductType);
                 }
             }
             update.set("endTime",endTime);
             update.set("updateTime",new Date());
             this.updateById(new ObjectId(vipMember.getId()),update);
         }
    }

    /**
     * @description 削减会员有效时间
     * @author cwj
     * @param [productType, endTime]
     * @return java.lang.Long
     * @date @time 2025/2/18 16:29
     **/
    private Long cutDownVipEndTime(Integer productType, Long endTime) {
        switch(productType){
            case 1:
                endTime = endTime - 30*24*60*60;
                break;
            case 2:
                endTime = endTime - 365*24*60*60;
                break;
            case 3:
                endTime = endTime - 150L*365*24*60*60;
                break;
            default:
                break;
        }
        return endTime;
    }


    /**
     * @description 更新VIP信息
     * @author cwj
     * @param [vo, vId]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2024/12/29 17:18
     **/
    public ResponseMap updateVipMember(VipMemberOpenReqBodyFormVo vo, String vId) {
        Update update = buildUpdate(vo.getOrderid(),vo.getGrade(),vo.getType(),vo.getIsVip(),vo.getIsAgreeVipProtocol());
        this.updateById(new ObjectId(vId),update);
        return ResponseMap.success();
    }

    /**
     * @description 构建更新会员Update对象
     * @author cwj
     * @param [vo]
     * @return org.springframework.data.mongodb.core.query.Update
     * @date @time 2024/12/30 16:51
     **/
    public Update buildUpdate(String orderid,Integer grade,Integer type,Integer isVip,Integer isAgreeVipProtocol) {
        long currentTime = System.currentTimeMillis()/1000;
        Update update = new Update();
        if(!StringUtils.isEmpty(orderid)){
            update.set("orderid", orderid);
        }
        update.set("grade",ObjectUtils.isEmpty(grade)?1: grade);
        update.set("type", type);
        update.set("endTime",transferEndTime(type, currentTime));
        if(ObjectUtils.isNotEmpty(isVip)){
            update.set("isVip",isVip);
        }
        update.set("isAgreeVipProtocol",ObjectUtils.isEmpty(isAgreeVipProtocol)?1: isAgreeVipProtocol);
        update.set("updateTime",new Date());
        return update;
    }

    /**
     * @description 保存VIP信息
     * @author cwj
     * @param [vo, currentTime, date]
     * @return void
     * @date @time 2024/12/29 16:44
     **/
    public ResponseMap saveVipMember(VipMemberOpenReqBodyFormVo vo) {
        VipMember vipMember = buildVipMember(vo.getOpenid(),vo.getOrderid(),vo.getPId(),vo.getIsAgreeVipProtocol(),vo.getGrade(),vo.getType(),vo.getIsVip(),0);
        this.save(vipMember);
        return ResponseMap.success();
    }

    
    /**
     * @description 
     * @author cwj
     * @param [openid, orderid, isAgreeVipProtocol, grade, type, isVip, currentTime, date]
     * @return com.iot08.common.entity.app.VipMember
     * @date @time 2024/12/30 16:47
     **/
    public VipMember buildVipMember(String openid,String orderid,String pId,Integer isAgreeVipProtocol, Integer grade,Integer type, Integer isVip,Integer isOnTrailVip) {
        long currentTime = System.currentTimeMillis()/1000;
        VipMember vipMember = new VipMember();
        vipMember.setOpenid(openid);
        vipMember.setOrderid(orderid);
        vipMember.setPId(pId);
        vipMember.setIsAgreeVipProtocol(isAgreeVipProtocol);
        vipMember.setBeginTime(currentTime);
        vipMember.setGrade(ObjectUtils.isEmpty(grade)?1: grade);
        vipMember.setType(type);
        vipMember.setEndTime(transferEndTime(type, currentTime));
        vipMember.setIsVip(ObjectUtils.isEmpty(isVip)?0: isVip);
        vipMember.setIsOnTrialVip(ObjectUtils.isEmpty(isOnTrailVip)?0:isOnTrailVip);
        vipMember.setCreateTime(new Date());
        vipMember.setUpdateTime(new Date());
        return vipMember;
    }

    /**
     * @description 计算机制日期
     * @author cwj
     * @param [vo, currentTime]
     * @return java.lang.Long
     * @date @time 2024/12/29 16:38
     **/
    private Long transferEndTime(Integer type, long currentTime) {
        long deadline = currentTime+30*24*60*60;
        switch(type){
            case 1:
                deadline = currentTime + 30*24*60*60;
                break;
            case 2:
                deadline = currentTime + 365*24*60*60;
                break;
            case 3:
                deadline = currentTime + 150L*365*24*60*60;
                break;
            case 4:
                deadline = currentTime + 182*24*60*60;
                break;
            default:
                break;
        }
        return deadline;
    }


    /**
     * @description 查询用户会员信息
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2024/12/29 15:54
     **/
    public ResponseMap queryVipMember(BodyFormAccount vo){
        VipMember vipMember = this.queryVipMemberByOpenid(vo.getOpenid());
        VimMemberResp vipMemberResp = new VimMemberResp();
        ResponseMap res = ResponseMap.success();
        if(ObjectUtils.isEmpty(vipMember)) {
            res.put("vipMember",vipMemberResp);
            return res;
        }
        long currentTime = System.currentTimeMillis()/1000;
        //会员所拥有的服务（产品）信息
        vipMemberResp.setPid(vipMember.getPId());
        vipMemberResp.setOrderid(vipMember.getOrderid());
        vipMemberResp.setGrade(vipMember.getGrade());
        vipMemberResp.setType(vipMember.getType());
        vipMemberResp.setIsOnTrialVip(ObjectUtils.isEmpty(vipMember.getIsOnTrialVip())?0: vipMember.getIsOnTrialVip());
        //已经过期了则需要更新会员状态数据
        if(currentTime > vipMember.getEndTime()){
            vipMemberResp.setIsVip(2);
            //更新会员状态为2，失效会员
            if(vipMember.getIsVip() != 2){
                Update update = new Update();
                //更新会员为过期
                update.set("isVip",2);
                this.updateById(new ObjectId(vipMember.getId()),update);
            }
        } else {
            vipMemberResp.setIsVip(vipMember.getIsVip());
        }
        vipMemberResp.setDeadline(DateUtil.stampToDateStr(vipMember.getEndTime()*1000,DateUtil.FORMAT_4));
        res.put("vipMember",vipMemberResp);
        return res;
    }

    /**
     * @description
     * @author cwj
     * @param [openids]
     * @return java.util.List<com.iot08.common.entity.app.VipMember>
     * @date @time 2025/1/8 14:23
     **/
    public List<VipMember> queryVipMemberByOpenids(Set<String> openids){
         long currentTime = System.currentTimeMillis()/1000;
         Query query = new Query(Criteria.where("openid").in(openids).and("isVip").is(1).and("endTime").gte(currentTime));
         return this.find(query);
    }
    
    /**
     * @description 根据openid查询有效的Vip信息
     * @author cwj
     * @param [openid]
     * @return com.iot08.common.entity.app.VipMember
     * @date @time 2024/12/29 16:23
     **/
    public VipMember queryVipMemberByOpenid(String openid) {
        long currentTime = System.currentTimeMillis()/1000;
        return this.findOne(new Query(Criteria.where("openid").is(openid).and("isVip").is(1).and("endTime").gte(currentTime)));
    }


    /**
     * @description 查询会员协议
     * @author cwj
     * @param []
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2024/12/29 15:53
     **/
    public String queryVipMemberServiceProtocol(){
        return "https://" + domain + "/agreement/vipServiceProtocol.html";
    }

    /**
     * @description 检查用户会员是否快到期或用户是否为还未充值过会员，未充值过则为首充，首充则返回首充状态
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/4/22 8:57
     **/
    public ResponseMap checkVipExpire(String openId) {
        Integer vipExpireStatus = 0;
        String content = "";
        String title = "";
        //查询会员信息
        VipMember vipMember = this.queryVipMemberByOpenId(openId);
        Long currentTime = System.currentTimeMillis()/1000;
        //检查是否为充值会员
        if(ObjectUtils.isEmpty(vipMember) || Integer.valueOf(0).equals(vipMember.getIsVip())){
            //非会员，检查是否N天内已经有过提醒
            JSONObject jsonObject = firstRechargeRemindDealWith(openId, currentTime);
            content = jsonObject.getString("content");
            vipExpireStatus = jsonObject.getInteger("vipExpireStatus");
            title = jsonObject.getString("title");
        } else if(Integer.valueOf(1).equals(vipMember.getIsVip()) && StringUtils.isEmpty(vipMember.getOrderid()) && Integer.valueOf(1).equals(vipMember.getIsOnTrialVip())){
            //免费会员
            JSONObject jsonObject = firstRechargeRemindDealWith(openId, currentTime);
            content = jsonObject.getString("content");
            vipExpireStatus = jsonObject.getInteger("vipExpireStatus");
            title = jsonObject.getString("title");
        } else {
            Long recentlyTime = 7*24*3600L;
            Long period = 1*30*60L;
            title = "续费专享";
            content = "您的会员即将过期，续费立享专属权益";
            //查询配置
            FirstRechargeRenewRemindSetting frrs = firstRechargeRenewRemindSettingService.queryByType(1);
            if(!ObjectUtils.isEmpty(frrs)) {
                if(!ObjectUtils.isEmpty(frrs.getRecentlyTime())){
                    recentlyTime = frrs.getRecentlyTime();
                }
                if(!ObjectUtils.isEmpty(frrs.getPeriod())) {
                    period = frrs.getPeriod();
                }
                if(!StringUtils.isEmpty(frrs.getTitle())){
                    title = frrs.getTitle();
                }
                if(!StringUtils.isEmpty(frrs.getContent())){
                    content = frrs.getContent();
                }
            }
            //充值的会员，检查会员是否快到期
            if(currentTime > vipMember.getEndTime()){
                //充值的会员已经到期，无需提醒
                vipExpireStatus = 1;
                title = "";
                content = "";
            }else if((vipMember.getEndTime() - currentTime) <=  recentlyTime){
                LocalDate currentDate = LocalDate.now();
                LocalDate yesterday = currentDate.minusDays(1);
                String yesterdayKey = openId+"-"+ yesterday.format(DateTimeFormatter.ofPattern(DateUtil.FORMAT_4)) + Constant.VIP_RECENT_EXPIRE_POSTFIX;
                String currentkey = openId+"-"+ currentDate.format(DateTimeFormatter.ofPattern(DateUtil.FORMAT_4)) + Constant.VIP_RECENT_EXPIRE_POSTFIX;
                log.info("今天key的值为：{}",currentkey);
                if(StringUtils.isEmpty(redisFilterService.get(yesterdayKey))) {
                    redisFilterService.del(yesterdayKey);
                }
                String remindTime = redisFilterService.get(currentkey);
                if(StringUtils.isEmpty(remindTime)){
                    //充值会员临近到期，在周期内还未提醒，需要提醒
                    vipExpireStatus = 4;
                    redisFilterService.setex(currentkey,period.intValue(),currentTime.toString());
                } else {
                    Long beforeRemindTime = Long.valueOf(remindTime);
                    if(currentTime-beforeRemindTime>period) {
                        //充值会员临近到期，上一次提醒已经超过周期了，需要提醒
                        vipExpireStatus = 3;
                        redisFilterService.del(currentkey);
                        redisFilterService.setex(currentkey,period.intValue(),currentTime.toString());
                    } else {
                        //充值会员临近到期，但在周期时间内已经提醒过了，无需再提醒
                        vipExpireStatus = 2;
                        title = "";
                        content = "";
                    }
                }
            }else {
                //是充值的会员，但会员还未临近到期，无需提醒
                vipExpireStatus = 0;
                title = "";
                content = "";
            }
        }
        //查询特权信息
        List<VipPrivilegeResp> privileges = vipPrivilegeService.queryAllVipPrivilegeResp();
        if(!CollectionUtils.isEmpty(privileges)) {
            privileges = privileges.stream().filter(p->!p.getName().equals("送碎屏险")).collect(Collectors.toList());
        }
        ResponseMap res = ResponseMap.success();
        res.put("vipExpireStatus",vipExpireStatus);
        res.put("privileges",privileges);
        if(!StringUtils.isEmpty(title)){
            res.put("title",title);
        }
        if(!StringUtils.isEmpty(content)) {
            res.put("content",content);
        }
        return res;
    }

    /**
     * @description 处理是否处于首次充值提醒的状态
     * @author cwj
     * @param [openId, currentTime]
     * @return com.alibaba.fastjson.JSONObject
     * @date @time 2025/4/22 14:38
     **/
    private JSONObject firstRechargeRemindDealWith (String openId, Long currentTime){
        String content = "首充月会员特惠仅需4.9元，享受以下专享特权";
        String title="成为VIP会员";
        int vipExpireStatus = 7;
        Long period = 1*1*30*60L;
        JSONObject jsonObject = new JSONObject();
        String key = openId+Constant.FIRST_CHARGE_VIP_REMIND;
        String remindTime = redisFilterService.get(key);
        //查询首充配置
        FirstRechargeRenewRemindSetting frrs = firstRechargeRenewRemindSettingService.queryByType(0);
        if(!ObjectUtils.isEmpty(frrs)) {
            if(!StringUtils.isEmpty(frrs.getContent())){
               content = frrs.getContent();
            }
            if(!ObjectUtils.isEmpty(frrs.getPeriod())){
                period = frrs.getPeriod();
            }
        }
        if(StringUtils.isEmpty(remindTime)) {
            //用户还未充值过会员，且之前未提醒过，提醒用户首次月会员充值
            vipExpireStatus = 5;
            redisFilterService.setex(key,period.intValue(),currentTime.toString());
        } else {
            Long beforeRemindTime = Long.valueOf(remindTime);
            if(currentTime-beforeRemindTime>period){
                //用户还未充值过会员，之前提醒过，但已经超过提醒周期，需要再次提醒用户首次充值月会员
                vipExpireStatus = 6;
                redisFilterService.del(key);
                redisFilterService.setex(key,period.intValue(),currentTime.toString());
            } else {
                //用户还未充值过会员，之前提醒过，目前还在提醒周期内，无需再次提醒
                //vipExpireStatus = 7;
                content = "";
                title = "";
            }
        }
        jsonObject.put("content", content);
        jsonObject.put("vipExpireStatus", vipExpireStatus);
        jsonObject.put("title",title);
        return jsonObject;
    }

}
